package com.tengju.bff.interfaces.covercharge.controller.portal;

import com.idanchuang.component.base.JsonResult;
import com.idanchuang.component.redis.util.DRedisLocks;
import com.tengju.bff.interfaces.covercharge.constant.BizConstants;
import com.tengju.bff.interfaces.covercharge.entity.vo.contract.*;
import com.tengju.bff.interfaces.covercharge.service.IServiceContractService;
import com.tengju.bff.interfaces.covercharge.util.UserUtils;
import com.tengju.bff.interfaces.covercharge.util.validator.ValidaUtil;
import com.tengju.bff.interfaces.shared.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liang_shi
 * @date 2020/7/15 15:52
 * @description 签署合同服务
 */
@RestController
@Slf4j
@RequestMapping("/api/signature")
@Api(value = "signature", tags = "签署服务")
public class SignatureController {

    @Autowired
    private IServiceContractService contractService;


    @ResponseBody
    @PostMapping("/contract")
    @ApiOperation(value = "签署合同", notes = "签署合同")
    public ApiResponse<SignContractResVo> signContract(HttpServletRequest request, @RequestBody SignContractReqVo reqVo) {
        //平台标识
        String platformCode = UserUtils.getPlatformCode(request);
        //获取用户Id
        Integer idCode = UserUtils.getIdCode(request);
        ValidaUtil.baseReqParamCheck(reqVo);
        return DRedisLocks.runWithLock(BizConstants.SIGN_CONTRACT + idCode, 500, () ->
                contractService.signContract(platformCode, idCode, reqVo));
    }


    @ResponseBody
    @PostMapping("/lock/contract")
    @ApiOperation(value = "锁定签署合同", notes = "锁定签署合同")
    public ApiResponse<Boolean> lockSignContract(HttpServletRequest request, @RequestBody SignContractLockReqVo reqVo) {
        ValidaUtil.baseReqParamCheck(reqVo);
        //平台标识
        String platformCode = UserUtils.getPlatformCode(request);
        //获取用户Id
        Integer idCode = UserUtils.getIdCode(request);
        return DRedisLocks.runWithLock(BizConstants.LOCK_SIGN_CONTRACT + idCode, 500, () ->
                contractService.lockSignContract(platformCode, idCode,reqVo.getContractId()));
    }


    @ResponseBody
    @PostMapping("/contract/info")
    @ApiOperation(value = "查询签署合同信息", notes = "查询签署合同信息")
    public ApiResponse<SignContractInfoResVo> querySignContractInfo(HttpServletRequest request, @RequestBody SignContractInfoReqVo reqVo) {
        //平台标识
        String platformCode = UserUtils.getPlatformCode(request);
        //获取用户Id
        Integer idCode = UserUtils.getIdCode(request);
        ValidaUtil.baseReqParamCheck(reqVo);
        return ApiResponse.newSuccess(contractService.querySignContractInfo(platformCode, idCode, reqVo));
    }


}
